<?php

namespace app\http\middleware;

use think\auth\Auth;

class AdminLoginCheck
{
    public function handle($request, \Closure $next)
    {
        if (cookie('?adminId')) {
            $adminInfo = model('Admin')->where('id', cookie('adminId'))->find();
            if ($adminInfo['status'] == 0) {
                cookie('adminId', null);
                return redirect('admin/login/login');
            }

            $isAccess = false;
            $auth = new Auth();
            $dispatch = request()->module() . '/' . request()->controller() . '/' . request()->action();
            if ($auth->check($dispatch, cookie('adminId'))) {
                $isAccess = true;
            } else {
                $isAccess = false;
            }

            if (in_array(cookie('adminId'), [config('extra.super_admin')])) {
                $isAccess = true;
            }

            if (stristr(request()->action(), 'wdl')) {
                $isAccess = true;
            }

            if (request()->controller() == 'Home' && request()->action() == 'index') {
                $isAccess = true;
            }

            if (request()->controller() == 'Home' && request()->action() == 'home') {
                $isAccess = true;
            }

            if ($isAccess) {
                $sysInfo = model('System')->find();
                $groupId = model('AuthGroupAccess')->where('uid', cookie('adminId'))->value('group_id');
                $ruleIds = model('AuthGroup')->where('id', $groupId)->value('rules');
                $ruleIdList = explode(',', $ruleIds);
                if (in_array(cookie('adminId'), [config('extra.super_admin')])) {
                    $where = [
                        'pid' => 0,
                        'is_show' => 1
                    ];
                } else {
                    $where = [
                        'id' => $ruleIdList,
                        'pid' => 0,
                        'is_show' => 1
                    ];
                }
                $oneMenu = model('AuthRule')->where($where)->order('sort', 'asc')->select()->each(function ($item, $key) use ($ruleIdList) {
                     if (in_array(cookie('adminId'), [config('extra.super_admin')])) {
                        $childWhere = ['pid' => $item['id'], 'is_show' => 1];
                    } else {
                        $childWhere = ['pid' => $item['id'], 'is_show' => 1, 'id' => $ruleIdList];
                    }
                    $item['child'] = model('AuthRule')->where($childWhere)->order('sort', 'asc')->select();
                });
                $viewData = [
                    'sysInfo' => $sysInfo,
                    'adminInfo' => $adminInfo,
                    'menuList' => $oneMenu
                ];
                \think\facade\View::share($viewData);
                return $next($request);
            } else {
                if (request()->isAjax()) {
                    return json(['code' => 0, 'msg' => '无权限操作']);
                } else {
                    return redirect('admin/home/wdlNoAuth');
                }
            }
        } else {
            return redirect('admin/login/login');
        }
    }
}
